
module.exports = function(maxCount) {
    let count = 0;
    let max = maxCount == undefined ? 128 : maxCount;
    let queue = [];
    let map = {};

    this.get = function(key) {
        let idx = queue.indexOf(key);
        if (idx != -1) {
            queue.splice(idx, 1);
            queue.push(key);
            return map[key];
        }
        else return undefined;
    };

    this.put = function (key, value) {
        let idx = queue.indexOf(key);
        if (idx == -1) {
            count++;
            while(count > max) {
                delete map[queue.shift()];
                count--;
            }
        } else {
            queue.splice(idx, 1);
        }
        queue.push(key);
        map[key] = value;
    };
    
    this.remove = function (key) {
        let idx = queue.indexOf(key);
        if (idx == -1) return undefined;
        count--;
        queue.splice(idx, 1);
        return delete map[key];
    };
    
    this.clear = function() {
        count = 0;
        queue = [];
        map = {};
    };

    this.size = function () {
        return count;
    };
    
    this.capacity = function (num) {
        if (num != undefined) max = num;
        else return max;
    };
    
}

